package com.rong.ruiji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rong.ruiji.common.CustomException;
import com.rong.ruiji.entity.Category;
import com.rong.ruiji.entity.Dish;
import com.rong.ruiji.entity.SetMeal;
import com.rong.ruiji.mapper.CategoryMapper;
import com.rong.ruiji.mapper.SetMealMapper;
import com.rong.ruiji.service.CategoryService;
import com.rong.ruiji.service.DishService;
import com.rong.ruiji.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService{

    @Autowired
    private DishService dishService ;

    @Autowired
    private SetMealService setMealService;

    /**
     * 根据id删除分类，删除前需要进行判断
     * @param id
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);

        //判断当前分类是否关联了菜品
        int count = dishService.count(dishLambdaQueryWrapper);
        if(count>0) {
            throw  new CustomException("当前分类关联了菜品，不能删除");
        }
        //判断当前分类是否关联了套餐
        LambdaQueryWrapper<SetMeal> setMealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setMealLambdaQueryWrapper.eq(SetMeal::getCategoryId,id);
        int countMeal = setMealService.count(setMealLambdaQueryWrapper);
        log.info(String.valueOf(countMeal));
        if(countMeal > 0) {
            throw  new CustomException("当前分类关联了套餐   ，不能删除");
        }
        //无关联，正常删除
        super.removeById(id);
    }
}
